//
//  ReturnDetailViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/27.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ReturnDetailViewController.h"
#import "SocketRequest.h"
#import "Singleton.h"
#import "WaitReturnExpress.h"
#import "WaitReturnCell.h"

@interface ReturnDetailViewController ()<SocketRequestDelegate> {
    SocketRequest *_socketRequest;
    
    WaitReturnExpress *_express;
}

@end

@implementation ReturnDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"退件查询";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self getReturnExpressSocket];
}

//获取待退件快件
- (void)getReturnExpressSocket {
    [self.tableView makeLoading];
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STS7"]];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.websiteId] length:64];
    [mutableData appendBytes:[SocketMethod checkLegal:self.expresser.expressCompanyId] length:64];
    [mutableData appendBytes:[SocketMethod checkLegal:self.expressId] length:64];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

//退件
- (void)returnExpressSocket {
    [ToastActivity makeActivity:@"退件中..." InView:self.view];
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STS8"]];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.websiteId] length:64];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.userId] length:64];
    NSString *content = [NSString stringWithFormat:@"%@,%@,%@",[SocketMethod checkNull:self.expresser.expressCompanyId],[SocketMethod checkNull:self.expresser.expresserId],[SocketMethod checkNull:_express.inwayExpressId]];
    NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
    [mutableData appendData:[SocketMethod hexDataWithLength:contentData.length]];
    [mutableData appendData:contentData];
    [_socketRequest addSocketData:mutableData SocketTag:2];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_express) {
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 143;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"WaitReturnCell";
    WaitReturnCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"WaitReturnCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell reloadWaitReturnCell:_express];
    return cell;
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    if (socket.socketTag == 1) {
        if ([code isEqualToString:SuccessSocket]) {
            NSString *content = [SocketMethod stringDecryHexData:data];
            DLog(@"content:%@",content);
            NSArray *array = [content componentsSeparatedByString:@"|"];
            for (NSString *string in array) {
                if (string.length > 0) {
                    _express = [[WaitReturnExpress alloc] initWithString:string];
                    self.tableView.backgroundView = nil;
                    _bottomView.frame = CGRectMake(0, 0, 320, 80);
                    self.tableView.tableFooterView = _bottomView;
                    [self.tableView reloadData];
                    break;
                }
            }
        } else {
            NSString *tip = [NSString stringWithFormat:@"%@,请重新扫描运单号。",msg];
            [self.tableView makeTip:tip Title:@"重新扫描" Target:self Selector:@selector(backLastViewController)];
        }
    } else if (socket.socketTag == 2) {
        if ([code isEqualToString:SuccessSocket]) {
            if ([_delegate respondsToSelector:@selector(successReturnExpressWithWaitReturnExpress:)]) {
                [_delegate successReturnExpressWithWaitReturnExpress:_express];
            }
            [ToastActivity makeSuccess:@"退件成功" InView:self.view];
            [self performSelector:@selector(backLastViewController) withObject:nil afterDelay:1];
        } else {
            [ToastActivity makeError:msg InView:self.view];
        }
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    if (socket.socketTag == 1) {
        [self.tableView makeTip:msg Title:@"重新加载" Target:self Selector:@selector(getReturnExpressSocket)];
        [DSToast showText:msg InView:self.view];
    } else if (socket.socketTag == 2) {
        [ToastActivity makeError:msg InView:self.view];
    }
}

#pragma mark NibAction
- (IBAction)sureReturnExpress:(id)sender {
    [self returnExpressSocket];
}

- (void)dealloc {
    _socketRequest = nil;
    _express = nil;
    self.expresser = nil;
}

@end
